// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: EHZ Setup Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 40 : TYPE_EHZSETUP

// Function declarations
reserve function EHZSetup_Function97
reserve function EHZSetup_Function98
reserve function EHZSetup_Function99
reserve function EHZSetup_Function100
reserve function SLZSetup_Function93
reserve function SLZSetup_Function94

// Static Values
public value EHZSetup_value33 = 0;

// Tables
public table EHZSetup_table39
	508, 24, 509, 10, 510, 12, 511, 24, 510, 12, 509, 10
end table

public table EHZSetup_table40
	512, 128, 513, 20, 512, 8, 513, 8, 512, 8, 513, 8
end table

public table EHZSetup_table41
	514, 128, 515, 12, 514, 12, 515, 12, 514, 6, 515, 6, 514, 6, 515, 6
end table

public table EHZSetup_table42
	516, 8, 517, 8
end table

public table EHZSetup_table43
	518, 128, 519, 8, 518, 8, 519, 8, 518, 8, 519, 12, 518, 12, 519, 12
	
end table

public table EHZSetup_table38
	1, 2, 1, 3, 1, 2, 2, 1, 2, 3, 1, 2, 1, 2, 0, 0
	2, 0, 3, 2, 2, 3, 2, 2, 1, 3, 0, 0, 1, 0, 1, 3
	1, 2, 1, 3, 1, 2, 2, 1, 2, 3, 1, 2, 1, 2, 0, 0
	2, 0, 3, 2, 2, 3, 2, 2, 1, 3, 0, 0, 1, 0, 1, 3
end table

private table EHZSetup_table0
	0x440000, 0x2910000, 32, 1, 40, 27, 8, 39, 56, 1, 40, 9, 8, 40, 56, 1, 40, 6, 8, 8, 56, 1, 40, 6, 8, 175, 56, 1, 40, 7, 8, 111, 0, 2, 4, 35, 52, 1, 36, 7, 4, 73, 0, 4, 8, 4, 56, 1, 40, 7, 8, 68, 0, 5, 4, 30, 0, 8, 2, 4, 50, 1, 34, 1, 50, 1, 32, 4, 0, 15, 8, 56, 0, 1, 2, 32, 50, 1, 34, 1, 42, 1, 40, 3, 8, 255, 8, 97, 0, 12, 4, 5, 0, 26, 8, 21, 56, 1, 40, 9, 8, 255, 8, 4, 10, 3, 2, 7, 10, 2
end table

private table EHZSetup_table1
	0x440000, 0x2950000, 32, 13, 40, 38, 8, 47, 56, 1, 40, 12, 8, 224, 56, 1, 40, 5, 8, 69, 56, 1, 40, 8, 32, 2, 0, 2, 8, 12, 0, 28, 8, 16, 0, 1, 8, 255, 8, 5, 56, 1, 40, 24, 8, 218, 56, 1, 40, 9, 8, 50, 0, 1, 4, 7, 0, 16, 8, 174, 56, 1, 40, 12, 8, 7, 56, 1, 40, 8, 8, 4, 56, 1, 40, 5, 8, 3, 4, 2, 52, 1, 36, 5, 4, 4, 52, 1, 32, 5, 0, 4, 4, 1, 52, 1, 36, 3, 4, 4, 52, 1, 36, 4, 4, 5, 52, 1, 36, 3
	4, 1, 0, 2, 8, 2, 56, 1, 40, 3, 32, 2, 0, 1, 8, 5, 0, 5, 8, 3, 56, 1, 40, 2, 8, 1, 0, 9, 8, 5, 0, 15, 8, 22, 0, 3, 4, 2, 52, 1, 36, 9, 32, 2, 40, 8, 8, 4, 56, 1, 40, 4, 32, 1, 0, 3, 48, 1, 32, 6, 0, 4, 48, 1, 32, 5, 0, 1, 8, 3, 0, 1, 48, 1, 32, 5, 0, 3, 48, 1, 32, 3, 40, 3, 8, 2, 0, 1, 48, 1, 32, 5, 0, 5, 48, 1, 32, 4, 0, 4, 48, 1, 32, 4, 0, 5, 48, 1, 32, 4, 0, 4, 48, 1, 32, 5, 0, 4, 48, 1, 32, 4, 0, 5, 48, 1, 32, 4, 0, 5, 48, 1, 32, 4, 0, 5, 48, 1, 32, 5, 0, 3, 48, 1, 32, 4, 0, 4, 48, 1, 32, 5, 0, 3, 48, 1, 32, 5, 0, 3, 48, 1, 32, 6, 0, 3, 48, 1, 32, 4, 0, 4, 48, 1, 32, 4, 0, 4, 48, 1, 32, 4, 0, 4, 48, 1, 32, 4, 40, 2, 8, 39, 56, 1, 40, 12, 8, 13, 0, 17, 8, 4, 0, 12, 4, 11, 8, 8
end table

private table EHZSetup_table2
	0x440000, 0x2910000, 0, 18, 8, 77, 56, 1, 40, 13, 8, 70, 56, 1, 40, 14, 8, 3, 0, 22, 4, 4, 0, 3, 48, 1, 32, 16, 40, 3, 8, 1, 0, 30, 48, 1, 32, 38, 40, 149, 8, 11, 0, 3, 4, 6, 0, 4, 4, 5, 0, 5, 8, 13, 0, 14, 4, 89, 0, 6, 8, 44, 10, 1, 2, 13, 0, 52, 8, 59, 56, 1, 40, 17, 8, 18, 0, 4, 4, 33, 0, 1, 8, 105, 56, 1, 40, 30, 8, 45, 4, 6, 0, 52, 8, 6, 56, 1, 40, 19, 8, 54, 10, 6, 2, 17, 10, 2, 8, 58, 56, 1, 40, 4, 8, 197, 56, 1, 41, 10, 9, 14, 57, 1, 41, 40, 33, 4, 1, 126, 9, 12, 8, 6, 56, 1, 40, 12, 
end table

function EHZSetup_Function97
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			SwapMusicTrack("EmeraldHill_F.ogg", 0, 0x1DD80, 0x1F40)
			break
		case 2
			SetMusicTrack("EmeraldHill_F.ogg", 0, 0x1DD80)
			SwapMusicTrack("Invincibility_F.ogg", 2, 0x78B1, 0x1F40)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("EmeraldHill_F.ogg", 0, 0x1DD80)
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function EHZSetup_Function98
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			SwapMusicTrack("EmeraldHill.ogg", 0, 0x254AE, 0x30D4)
			break
		case 2
			SetMusicTrack("EmeraldHill.ogg", 0, 0x254AE)
			SwapMusicTrack("Invincibility.ogg", 2, 0x9717, 0x30D4)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("EmeraldHill.ogg", 0, 0x254AE)
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			break
		end switch
	else
		stage.musicFlag = 2
	end if
end function


function EHZSetup_Function99
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			SwapMusicTrack("EmeraldHill2_F.ogg", 0, 0x136F4, 0x1F40)
			break
		case 2
			SetMusicTrack("EmeraldHill2_F.ogg", 0, 0x136F4)
			SwapMusicTrack("Invincibility_F.ogg", 2, 0x78B1, 0x1F40)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("EmeraldHill2_F.ogg", 0, 0x136F4)
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function EHZSetup_Function100
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			SwapMusicTrack("EmeraldHill2.ogg", 0, 0x184F4, 0x30D4)
			break
		case 2
			SetMusicTrack("EmeraldHill2.ogg", 0, 0x184F4)
			SwapMusicTrack("Invincibility.ogg", 2, 0x9717, 0x30D4)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("EmeraldHill2.ogg", 0, 0x184F4)
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			break
		end switch
	else
		stage.musicFlag = 2
	end if
end function


function SLZSetup_Function93
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			SwapMusicTrack("Starlight_F.ogg", 0, 0x10838, 0x1F40)
			break
		case 2
			SetMusicTrack("Starlight_F.ogg", 0, 0x10838)
			SwapMusicTrack("Invincibility_F.ogg", 2, 0x78B1, 0x1F40)
			break
		default
			SetMusicTrack("Starlight_F.ogg", 0, 0x10838)
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function SLZSetup_Function94
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			SwapMusicTrack("Starlight.ogg", 0, 0x1498C, 0x30D4)
			break
		case 2
			SetMusicTrack("Starlight.ogg", 0, 0x1498C)
			SwapMusicTrack("Invincibility.ogg", 2, 0x9717, 0x30D4)
			break
		default
			SetMusicTrack("Starlight.ogg", 0, 0x1498C)
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			break
		end switch
	else
		stage.musicFlag = 2
	end if
end function


event ObjectMain
	
	switch stage.actNum
		case 3
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				temp0 = object[currentPlayer].collisionRight
				temp0 <<= 16
				temp0 += object[currentPlayer].xpos
				temp1 = stage.curXBoundary2
				temp1 <<= 16
				if temp0 > temp1
					object[currentPlayer].xvel = 0
					object[currentPlayer].speed = 0
					object[currentPlayer].xpos = temp1
					temp0 = object[currentPlayer].collisionRight
					temp0 <<= 16
					object[currentPlayer].xpos -= temp0
				end if
			next			
			break
	end switch
	
	if options.gameMode == 10
		if stage.seconds > 29
			if stage.timeEnabled == 1
				stage.timeEnabled = 0
				stage.pauseEnabled = 0
				stage.minutes = 0
				stage.seconds = 30
				stage.milliSeconds = 0
				object[30].type = TypeName[Blank Title Card]
				object[30].priority = PRIORITY_ACTIVE_PAUSED
				object[30].drawOrder = 6
				object[30].state = 7
				stage.state = 3
			end if
		end if	
	end if
	
	if object.state < 5
		object.value1++
		if object.value1 == 8
			object.value1 = 0
			GetPaletteEntry(0, 159, temp1)
			GetPaletteEntry(0, 158, temp0)
			SetPaletteEntry(0, 159, temp0)
			GetPaletteEntry(0, 148, temp0)
			SetPaletteEntry(0, 158, temp0)
			GetPaletteEntry(0, 147, temp0)
			SetPaletteEntry(0, 148, temp0)
			SetPaletteEntry(0, 147, temp1)
		end if
	end if
	object.value4++
	if object.value4 > 7
		tileLayer[1].deformationOffset++
		object.value4 = 0
	end if
	if object.value5 < 2
		GetTableValue(temp0, object.value6, EHZSetup_table39)
		Copy16x16Tile(502, temp0)
		temp0 += 12
		Copy16x16Tile(503, temp0)
		temp0 += 4
		Copy16x16Tile(504, temp0)
		object.value6++
		GetTableValue(object.value5, object.value6, EHZSetup_table39)
		object.value6++
		object.value6 %= 12
	else
		object.value5--
	end if
	if object.value7 < 2
		GetTableValue(temp0, object.value8, EHZSetup_table40)
		Copy16x16Tile(764, temp0)
		object.value8++
		GetTableValue(object.value7, object.value8, EHZSetup_table40)
		object.value8++
		object.value8 %= 12
	else
		object.value7--
	end if
	if object.value9 < 2
		GetTableValue(temp0, object.value10, EHZSetup_table41)
		Copy16x16Tile(765, temp0)
		object.value10++
		GetTableValue(object.value9, object.value10, EHZSetup_table41)
		object.value10++
		object.value10 &= 15
	else
		object.value9--
	end if
	if object.value11 < 2
		GetTableValue(temp0, object.value12, EHZSetup_table42)
		Copy16x16Tile(766, temp0)
		object.value12++
		GetTableValue(object.value11, object.value12, EHZSetup_table42)
		object.value12++
		object.value12 &= 3
	else
		object.value11--
	end if
	if object.value13 < 2
		GetTableValue(temp0, object.value14, EHZSetup_table43)
		Copy16x16Tile(767, temp0)
		object.value14++
		GetTableValue(object.value13, object.value14, EHZSetup_table43)
		object.value14++
		object.value14 &= 15
	else
		object.value13--
	end if
	if EHZSetup_value33 == 0
		if stage.debugMode == 0
			if specialStage.emeralds >= 127
				if options.stageSelectFlag == 0
					if options.gameMode == 0
						EHZSetup_value33 = 1
						//CallNativeFunction2(SetAchievement, 2, 100)
						achievement.trigger = 3 //Early Bird Special
					end if
					if options.gameMode == 1
						arrayPos1 = options.saveSlot
						arrayPos1 <<= 3
						arrayPos1 += 1000
						arrayPos1 += 4
						if saveRAM[arrayPos1] < 20
							EHZSetup_value33 = 1
							//CallNativeFunction2(SetAchievement, 2, 100)
							achievement.trigger = 3 //Early Bird Special
						end if
					end if
				end if
			end if
		end if
	end if
end event


event ObjectStartup
	//if stage.activeList != BONUS_STAGE
		if options.vsMode == 0
			switch stage.actNum
				case 3
					temp0 = tileLayer[0].xsize
					temp0 <<= 7
					stage.newXBoundary2 = temp0
					
					SetMusicTrack("Starlight.ogg", 0, 0x1498C)
					SpeedUpMusic = SLZSetup_Function93
					SlowDownMusic = SLZSetup_Function94
					break
				default
					SetMusicTrack("EmeraldHill.ogg", 0, 0x254AE)
					SpeedUpMusic = EHZSetup_Function97
					SlowDownMusic = EHZSetup_Function98
					break
			end switch
		else
			SetMusicTrack("EmeraldHill2.ogg", 0, 0x184F4)
			SpeedUpMusic = EHZSetup_Function99
			SlowDownMusic = EHZSetup_Function100
		end if
	//else
	//	SetMusicTrack("DeathEgg.ogg", 0, 1)
	//end if
	animalType1 = TypeName[Flicky]
	animalType2 = TypeName[Ricky]
	tileLayer[1].scrollPos = -0x80000
	arrayPos0 = 0
	while arrayPos0 < 576
		temp0 = arrayPos0
		temp0 &= 63
		GetTableValue(stage[arrayPos0].deformationData2, temp0, EHZSetup_table38)
		arrayPos0++
	loop
	object[10].type = TypeName[EHZ Setup]
	object[10].priority = PRIORITY_ACTIVE
	SetPaletteEntry(0, 192, 0)
	if options.attractMode == 1
		switch stage.playerListPos
		case 0
			PlayerObject_ReplayTable = EHZSetup_table0
			PlayerObject_ReplaySize = 110
			PlayerObject_ReplayLength = 0x654
			break
		case 1
			PlayerObject_ReplayTable = EHZSetup_table1
			PlayerObject_ReplaySize = 318
			PlayerObject_ReplayLength = 0x708
			break
		case 2
			PlayerObject_ReplayTable = EHZSetup_table2
			PlayerObject_ReplaySize = 138
			PlayerObject_ReplayLength = 0x708
			break
		end switch
		CallFunction(PlayerObject_InitReplay)
	end if
	switch stage.actNum
		case 0
		case 1
			object[383].xpos -= 0x8 0000
			object[383].ypos -= 0x10 0000
			object[384].xpos -= 0x8 0000
			object[384].ypos -= 0x10 0000
			//LoadPalette("EHZAct1Pal.act", 0, 128, 128, 256)
			break
		case 2
			SetTileLayerEntry(301, 0, 69, 9)
			ResetObjectEntity(408, TypeName[Blank Object], 0, 0, 0)
			
			object[424].value2 = object[424].value3 
			GetBit(temp0, object[424].propertyValue, 3)
			SetBit(object[424].propertyValue, 2, temp0)
			LoadPalette("EHZAct2Pal.act", 0, 128, 128, 256)
			break
	end switch
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
